<?php


namespace App\Helpers;


use App\Models\ActionLog;
use App\Models\Company;
use App\Models\Contribute;
use App\Models\Post;
use App\Models\School;
use App\Models\ShopDetail;
use App\Models\Student;
use App\Models\SubAccount\SubAccount;
use App\Models\SystemUser;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

trait Untils
{
    /**
     * @param $phone 手机号
     * @param $content 短信内容
     */
    protected function sms($phone,$content){
        $url = 'https://yqfk.yw.gov.cn/proxy/smssget';
        $data = [
            'code'=>md5(date('Y-m-d').'^&_124ada5sd'),
            'phone'=>$phone,
            'content'=>$content
        ];
        $response = Http::post($url,$data);
        return $response->json();
    }


    protected function findLastDepartment($request)
    {
        if ($request->department_three_id>0)
        {
            return $request->department_three_id;
        }
        if ($request->department_two_id>0) {
            return $request->department_two_id;
        }
        if ($request->department_one_id>0){
            return $request->department_one_id;
        }
        return 0;
    }
    //根据部门获取未读用户列表
    protected function getUnreadUser($shop_id){
        $department_id = ShopDetail::query()->where('id',$shop_id)->value('department_two_id');
        if($department_id){
            //获取二级管理员
            $users[] = SystemUser::query()
                ->where('department_two_id',$department_id)
                ->where('department_three_id',0)->value('id');
            $sub = SubAccount::query()->with(['staffBinds'=>function($query){
                $query->select('sub_account_id','staff_id');
            }])
                ->select('id')
                ->where('shop_id',$shop_id)->get();
            info(json_encode($sub));
            if($sub&&count($sub)>0){
                foreach ($sub as $n){
                    if($n->staffBinds){
                        if(count($n->staffBinds)>0){
                            foreach ($n->staffBinds as $v){
                                array_push($users,$v->staff_id);
                            }
                        }
                    }
                }
            }
            if($users){
                return $users;
            }
        }
        return null;
    }
}
